<?php


namespace app\cms\middleware;


use iflow\App;
use iflow\Container\implement\generate\exceptions\InvokeClassException;
use iflow\middleware\Middleware;

#[Middleware]
class SaasAccount {

    protected array $saasAccountState = [
        0 => '当前应用服务已过期',
        2 => '当前应用服务暂未生效'
    ];

    protected string $error = '';

    /**
     * Saas服务账户许可验证中间件
     * @param App $app
     * @param $next
     * @return mixed
     * @throws InvokeClassException
     */
    public function handle(App $app, $next): mixed {

        // 验证是否开启Saas服务
        if (config('app@saasEnable')) {
            $account = $app -> make(\app\cms\community\lib\saas\saasAccount::class) -> getSaasAccount();
            if (!$account) {
                return message() -> unauthorized_error('当前应用服务不存在');
            }

            $this->error = $this->saasAccountState[$account['status']] ?? '';
            if ($this->error) {
                return message() -> unauthorized_error($this->error);
            }
        }
        return $next($app);
    }
}